package com.lukk.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import com.lukk.constant.Const;
import com.lukk.dao.MerchantDAO;
import com.lukk.pojo.InternalWorker;
import com.lukk.pojo.Merchant;
import com.lukk.service.IMerchantService;
import com.lukk.util.JsonUtil;
/**
 * 
 * MerchantServiceImpl 上午10:25:17
 * 
 * Copyright zhaocj Inc. All rights reserved.
 * Love ME Like Justin Bieber.
 */
@Service("merchantService")
public class MerchantServiceImpl implements IMerchantService {
	private static Logger logger = LogManager.getLogger(MerchantServiceImpl.class.getName());
	@Resource
	private MerchantDAO merchantDAO;

	@Override
	public Map<String, Object> selectByPageAndQueryStatus(Integer offset, Integer limit, String sort, String order,
			String merchantName, String merchantPhone, String queryStatus) {
		Map<String, Object> queryMap = new HashMap<String, Object>();
		Map<String, Object> retsMap = new HashMap<String, Object>();
		queryMap.put("offset", offset);
		queryMap.put("limit", limit);
		queryMap.put("sort", sort);
		queryMap.put("order", order);
		queryMap.put("queryStatus", queryStatus);
		queryMap.put("merchantName", merchantName);
		queryMap.put("merchantPhone", merchantPhone);
		List<Merchant> beans = merchantDAO.selectMerchantByMap(queryMap);
		logger.debug("查询出来的所有商人信息：{}", JsonUtil.toJson(beans));
		retsMap.put("total", merchantDAO.selectMerchantCountByMap(queryMap));
		retsMap.put("rows", beans);
		return retsMap;
	}

	@Override
	public boolean modifyMerchantForInternalWorker(String merchants, String interID) {
		merchantDAO.modifyMerchantForInternalWorker(merchants, interID);
		return true;
	}

	@Override
	public Merchant selectByPrimaryKey(Integer merchantID) {
		return merchantDAO.selectByPrimaryKey(merchantID);
	}

	@Override
	public Map<String, Object> selectMerchantByInternalWorker(Integer offset, Integer limit, String sort, String order,
			String merchantName, String merchantPhone, HttpSession session) {
		InternalWorker internalWorker = (InternalWorker) session.getAttribute(Const.SESSION_USER);
		Map<String, Object> queryMap = new HashMap<String, Object>();
		Map<String, Object> retsMap = new HashMap<String, Object>();
		queryMap.put("offset", offset);
		queryMap.put("limit", limit);
		queryMap.put("sort", sort);
		queryMap.put("order", order);
		queryMap.put("queryStatus", 1);
		queryMap.put("merchantName", merchantName);
		queryMap.put("merchantPhone", merchantPhone);
		queryMap.put("internalWorkerID", internalWorker.getId());
		List<Merchant> beans = merchantDAO.selectMerchantByInternalWorker(queryMap);
		retsMap.put("total", merchantDAO.selectMerchantCountByInternalWorker(queryMap));
		retsMap.put("rows", beans);
		return retsMap;
	}
}